{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3345f18a-820f-4c92-bd5f-72c190eb3f0d",
   "metadata": {},
   "source": [
    "每段代码前执行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a08f8841-be03-402d-b317-70ea13def11a",
   "metadata": {},
   "source": [
    "添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1b4f98cd-3391-4393-8008-ad24c74822d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['/Users/will/opt/anaconda3/envs/d2l-zh-308/lib/python38.zip',\n",
       " '/Users/will/opt/anaconda3/envs/d2l-zh-308/lib/python3.8',\n",
       " '/Users/will/opt/anaconda3/envs/d2l-zh-308/lib/python3.8/lib-dynload',\n",
       " '',\n",
       " '/Users/will/opt/anaconda3/envs/d2l-zh-308/lib/python3.8/site-packages',\n",
       " '/Users/will/workspace/d2l-pytorch-zh']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "import sys\n",
    "\n",
    "# 获取当前工作目录\n",
    "current_directory = os.getcwd()\n",
    "\n",
    "# 获取上一级目录\n",
    "parent_directory = os.path.dirname(current_directory)\n",
    "\n",
    "# 仅当目标目录不在 sys.path 中时，才添加它\n",
    "if parent_directory not in sys.path:\n",
    "    sys.path.append(parent_directory)\n",
    "\n",
    "# 验证是否添加成功\n",
    "sys.path\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c9c342c-eedc-4918-b517-eae9c9291960",
   "metadata": {},
   "source": [
    "删除（不需要执行）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4dfaab9d-b301-4bfd-bb0a-89a2190f9a6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.remove(\"/Users/will/workspace/d2l-pytorch-zh\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83b143bb-0c02-4d94-913a-8077edc3fc02",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "d2l-zh-308",
   "language": "python",
   "name": "d2l-zh-308"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  },
  "required_libs": []
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
